// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: declaration of CPointerContainer.
//   $Id$



//	Occasionally, it is necessary to return pointers from functions that may or may not
//		have been created within that function via new, but that need to persist so that the 
//		calling function can use them
//	
//	Unfortunately, this means that some mechanism needs to exist in order to delete them.
//		Ideally, Reference Counts should be used, but if one of the pointers refers to a
//		mature class without a refcount mechanism, converting it to use refcounts 
//		can be very difficult, as this would require rewrites in code that references it.
//
//	The magic solution is to create one of these PointerContainers in the calling function, and pass it into
//		the called function byref.  The inner function can set m_bDeleteOnDestroy accordingly.

template <class T> class CPointerContainer
{
public:
	CPointerContainer()
		:m_bDeleteOnDestroy(false)
		,m_p(0)
	{
	};

	~CPointerContainer()
	{
		if(m_bDeleteOnDestroy)
			delete m_p;
	};

	operator T*() const throw()
	{
		return m_p;
	}


	bool m_bDeleteOnDestroy;
	T * m_p;


};